package channel

import (
	"context"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TenantChannelBankModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelBankModel(ctx context.Context, db *gorm.DB) *TenantChannelBankModel {
	return &TenantChannelBankModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelBankModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *TenantChannelBankModel) CreateBank(bank *schema.TenantChannelBank) error {
	res := model.getDb().Model(&schema.TenantChannelBank{}).Create(bank)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-CreateBank,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelBankModel) GetBankById(id int64) (*schema.TenantChannelBank, error) {
	var bank schema.TenantChannelBank
	res := model.getDb().Model(&schema.TenantChannelBank{}).Where("id = ?", id).First(&bank)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetBankById,err", err)
		return nil, err
	}
	return &bank, nil
}

func (model *TenantChannelBankModel) GetBankByCardNumber(cardNumber string) ([]schema.TenantChannelBank, error) {
	var bankList []schema.TenantChannelBank
	res := model.getDb().Model(&schema.TenantChannelBank{}).Where("card_number = ?", cardNumber).Find(&bankList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetBankByCardNumber,err", err)
		return nil, err
	}
	return bankList, nil
}

func (model *TenantChannelBankModel) UpdateBank(bank *schema.TenantChannelBank) error {
	res := model.getDb().Model(&schema.TenantChannelBank{}).Where("id = ?", bank.ID).Updates(bank)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateBank,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelBankModel) DeleteBank(id int64) error {
	res := model.getDb().Model(&schema.TenantChannelBank{}).Where("id = ?", id).Delete(&schema.TenantChannelBank{})
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-DeleteBank,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelBankModel) GetAllBank() ([]schema.TenantChannelBank, error) {
	var bankList []schema.TenantChannelBank
	res := model.getDb().Model(&schema.TenantChannelBank{}).Find(&bankList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetAllBank,err", err)
		return nil, err
	}
	return bankList, nil
}

func (model *TenantChannelBankModel) GetBanksByFilter(bankName, cardNumber, cardHolder string) ([]schema.TenantChannelBank, error) {
	var banks []schema.TenantChannelBank
	query := model.getDb().Model(&schema.TenantChannelBank{})

	if bankName != "" {
		query = query.Where("bank_name LIKE ?", "%"+bankName+"%")
	}
	if cardNumber != "" {
		query = query.Where("card_number LIKE ?", "%"+cardNumber+"%")
	}
	if cardHolder != "" {
		query = query.Where("card_holder LIKE ?", "%"+cardHolder+"%")
	}

	res := query.Find(&banks)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-GetBanksByFilter,err", err)
		return nil, err
	}
	return banks, nil
}

func (model *TenantChannelBankModel) GetBankIsLimit() (int32, error) {
	var bank schema.TenantChannelBank
	res := model.getDb().Model(&schema.TenantChannelBank{}).First(&bank)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetBankIsLimit,err", err)
		return 0, err
	}
	return bank.IsLimit, nil
}

func (model *TenantChannelBankModel) UpdateBankIsLimit(isLimit int32) error {
	var total int64
	model.getDb().Model(&schema.TenantChannelBank{}).Count(&total)
	if total == 0 {
		return nil
	}
	updates := schema.TenantChannelBank{
		IsLimit: isLimit,
	}
	res := model.getDb().Model(&schema.TenantChannelBank{}).Where("id>1").Updates(updates)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateBankIsLimit,err", err)
		return err
	}
	return nil
}
